package com.yangye.niodemo.rpc.transport;

import com.yangye.niodemo.rpc.protocol.PackageMsg;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yangye
 * @date 2020/12/29
 */
public class ResponseCallback {

	public static final Map<Long, CompletableFuture> map = new ConcurrentHashMap<>();

	public static void add(Long requestId, CompletableFuture cb) {
		map.putIfAbsent(requestId, cb);
	}

	public static void run(PackageMsg packageMsg) {
		CompletableFuture cf = map.get(packageMsg.getHeader().getRequestId());
		cf.complete(packageMsg.getBody().getResult());
		map.remove(packageMsg.getHeader().getRequestId());
	}
}
